import axios from 'axios';
import { ElMessage } from 'element-plus'

// request.js
// 创建新的axios实例
const service = axios.create({
    // 环境变量，需要在.env文件中配置
    baseURL: '',
    // headers: { Authorization: 'Bearer ' + localStorage.getItem('token') },
    // 超时时间暂定5s
    timeout: 5000,
});
// request.js
service.interceptors.request.use(
    config => {
        // 此处添加Loading
        config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token');
        return config;

    },
    error => {
        return Promise.reject(error);
    }
);
// request.js



// loading 次数
let loadingCount = 0;

service.interceptors.request.use(
    config => {
        // 加入Loading

        loadingCount++;
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
// request.js
service.interceptors.response.use(
    response => {
        // 关闭loading
        // loadingCount--;
        // if (loadingCount === 0) {
        //     // 关闭loading
        // }
        // console.log('res', response);
        if (response.data.code != 200) {
            ElMessage.error(response.data.msg);

        }
        return response;
    },
    error => {

        // 处理异常情况，根据项目实际情况处理或不处理
        if (error && error.response) {
            // 根据约定的响应码处理
            switch (error.response.status) {
                case 401:
                    ElMessage.error('登录状态已过期');
                    break;
                case 403:
                    error.message = '拒绝访问';
                    break;
                case 502:
                    error.message = '服务器端出错';
                    break;
                default:
                    error.message = `连接错误${error.response.status}`;
            }
        } else {
            // 超时处理
            error.message = '服务器响应超时，请刷新当前页';
        }

        return Promise.resolve(error.response);
    }
);
interface Options {
    method?: string;
    params?: any;
    data?: any;
}
// request.js
const Request = (url: string, options: Options) => {
    // console.log('axios', url, options);

    let method = options.method || 'get';
    let params = options.params || {};

    if (method === 'get' || method === 'GET') {
        return new Promise((resolve, reject) => {
            service
                .get(url,
                    {
                        params: params,
                    }
                )
                .then(res => {
                    if (res && res.data) {
                        resolve(res.data);
                    }
                })
                .catch(err => {
                    reject(err);
                });
        });
    } else if (method === 'post' || method === 'POST') {
        return new Promise((resolve, reject) => {
            service
                .post(url, params)
                .then(res => {
                    if (res && res.data) {
                        resolve(res.data);
                    }
                })
                .catch(err => {
                    reject(err);
                });
        });
    } else if (method === 'delete' || method === 'DELETE') {
        // console.log('delete');

        return new Promise((resolve, reject) => {
            service
                .delete(url, {
                    params: params,
                })
                .then(res => {
                    if (res && res.data) {
                        resolve(res.data);
                    }
                })
                .catch(err => {
                    reject(err);
                });
        });
    } else if (method === 'put' || method === 'PUT') {
        // console.log('put');

        return new Promise((resolve, reject) => {
            service
                .put(url, params)
                .then(res => {
                    if (res && res.data) {
                        resolve(res.data);
                    }
                })
                .catch(err => {
                    reject(err);
                });
        });
    }
};
// request.js
export default Request;
